//
//  NSString+LxString.m
//  LvXiMark
//
//  Created by apple on 16/4/22.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "NSString+LxString.h"

@implementation NSString (LxString)


-(CGSize)sizeInAre:(CGSize)are font:(UIFont *)font{
    NSDictionary * attribuite = @{NSFontAttributeName:font};
    CGSize textSize;
    textSize = [self boundingRectWithSize:are options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attribuite context:nil].size;
    return textSize;
    
}

-(CGSize)sizeInAre:(CGSize)are fontsize:(CGFloat)size{
    UIFont * font = [UIFont systemFontOfSize:size];
    NSDictionary * attribuite = @{NSFontAttributeName:font};
    CGSize textSize;
    textSize = [self boundingRectWithSize:are options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attribuite context:nil].size;
    return textSize;
}

-(NSString*)addHeader:(NSString*)headedr{
    return [NSString stringWithFormat:@"%@%@",headedr,self];
}
-(NSString *)urlDecoded
{
    NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)self, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    
    return decodedString;
}

/*
-(void)analysisUrl
{
    NSArray * array = [self componentsSeparatedByString:@"/"];
    NSDictionary * dict;
    if([self containsString:@"app/product"])
    {
         dict = @{@"type":@"product",@"id":array.lastObject};
    }
    else if([self containsString:@"app/cat"])
    {
         dict = @{@"type":@"category",@"id":array.lastObject};
    }
    else
    {
        dict = @{@"type":@"other"};
    }
    
    UIViewController * vc = [self getCurrentVC];
    
    if([dict[@"type"] isEqualToString:@"other"])
    {
        LXWebViewController * webVC = [[LXWebViewController alloc] initWithUrl:self];
        [vc.navigationController pushViewController:webVC animated:YES];
    }
    else if ([dict[@"type"] isEqualToString:@"product"])
    {
        LXLoaclCommodityDetailEntranceController * commodityDetailVC = [[LXLoaclCommodityDetailEntranceController alloc] init];
        commodityDetailVC.product_id = dict[@"id"];
        [vc.navigationController pushViewController:commodityDetailVC animated:YES];
    }
    else if ([dict[@"type"] isEqualToString:@"category"])
    {
        LXCommodityListController * listVC = [[LXCommodityListController alloc] init];
        listVC.cat_id = dict[@"id"];
        [vc.navigationController pushViewController:listVC animated:YES];
    }
}
*/
-(NSString*)time
{
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY年MM月dd日 HH:mm:ss"];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[self floatValue]];
    NSString * dateString = [formatter stringFromDate:date];
    return dateString;
}
-(NSDictionary*)detailDate
{
    NSMutableDictionary * mDict = [NSMutableDictionary dictionary];
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[self floatValue]];
    
    formatter.dateFormat = @"yyyy";
    NSString * year =[formatter stringFromDate:date];
    
    formatter.dateFormat = @"MM";
    NSString * month =[formatter stringFromDate:date];
    
    formatter.dateFormat = @"dd";
    NSString * day =[formatter stringFromDate:date];
    
    formatter.dateFormat = @"HH";
    NSString * hour =[formatter stringFromDate:date];
    
    formatter.dateFormat = @"mm";
    NSString * minute =[formatter stringFromDate:date];
    
    formatter.dateFormat = @"ss";
    NSString * second =[formatter stringFromDate:date];
    
    formatter.dateFormat = @"EEE";
    NSString * week =[formatter stringFromDate:date];
    [mDict setValue:year forKey:KYear];
    [mDict setValue:month forKey:KMonth];
    [mDict setValue:day forKey:KDay];
    [mDict setValue:hour forKey:KHour];
    [mDict setValue:minute forKey:KMinute];
    [mDict setValue:second forKey:KSecond];
    [mDict setValue:week forKey:KWeek];
    return mDict;
}



-(NSString *)sha1String{
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    
    CC_SHA1(data.bytes, (unsigned int)data.length, digest);
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    
    for(int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) {
        [output appendFormat:@"%02x", digest[i]];
    }
    return output;
}


+(NSString*)formatTime:(CGFloat)time{
    NSInteger hour = floor(time/3600);
    NSInteger mini = floor((time - hour*3600)/60);
    CGFloat second = (time - hour*3600-mini*60);
    return [NSString stringWithFormat:@"%ld:%ld:%.0f",hour,mini,second];
}


-(NSString *(^)(NSString *))add{
    return ^(id s){
        return  [NSString stringWithFormat:@"%@%@",self,s];
    };
}

@end


NSString * KYear = @"year";
NSString * KMonth = @"month";
NSString * KDay = @"day";
NSString * KHour = @"hour";
NSString * KMinute = @"minute";
NSString * KSecond = @"second";
NSString * KWeek = @"week";

